package com.example.pc.greenplants;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import cn.bmob.v3.Bmob;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.SaveListener;

public class Logon extends AppCompatActivity {

    private  EditText gphone;
    private EditText guser;
    private EditText gpsw;
    private EditText grpsw;
    private Button glogon;
    private Button greg;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_logon);
        gphone = findViewById(R.id.et_ph);
        guser = findViewById(R.id.et_user);
        gpsw = findViewById(R.id.et_paw);
        grpsw = findViewById(R.id.et_repaw);
        glogon = findViewById(R.id.btn_logon);
        greg = findViewById(R.id.btn_reg);
        greg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Logon.this, Login.class);
                startActivity(intent);
            }
        });
        add_logon();
        Bmob.initialize(this, "fa96b598aa0534b09043ff71c1b27ace");
    }

    public void add_logon() {
        glogon.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                String lphone = gphone.getText().toString();
                String luser = guser.getText().toString();
                String lpsw = gpsw.getText().toString();
                String lrpsw = grpsw.getText().toString();

                if (TextUtils.isEmpty(luser)) {
                    Toast.makeText(Logon.this, "用户名不能为空", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (TextUtils.isEmpty(lphone)) {
                    Toast.makeText(Logon.this,"手机号不能为空", Toast.LENGTH_SHORT).show();
                }
                    String telRegex = "[1][358]\\d{9}";
                     if (!lphone.matches(telRegex)) {
                         Toast.makeText(Logon.this, "手机号格式错误", Toast.LENGTH_SHORT).show();
                         return;
                     }
                if (lpsw.length() < 6) {
                    Toast.makeText(Logon.this,"密码不能少于6位", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (lpsw.length() > 16) {
                    Toast.makeText(Logon.this, "密码不能多于16位", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (!lpsw.equals(lrpsw)) {
                    Toast.makeText(Logon.this, "两次密码不一致，请重新输入", Toast.LENGTH_SHORT).show();
                    return;
                }
                final BmobUser buser = new BmobUser();
                buser.setUsername(luser);
                buser.setPassword(lpsw);
                buser.setMobilePhoneNumber(lphone);

                buser.signUp(new SaveListener<BmobUser>() {

                    @Override
                    public void done(BmobUser bmobUser, BmobException e) {
                        if (e == null) {
                            Intent intent = new Intent(Logon.this, Login.class);
                            startActivity(intent);
                            Toast.makeText(Logon.this, "注册成功", Toast.LENGTH_SHORT).show();
                        }else {
                            Toast.makeText(Logon.this, "用户名已被占用", Toast.LENGTH_SHORT).show();
                        }
                    }
                });

            }
        });
    }




}
